﻿using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace FrameWork
{
    public class WindowBase : WindowBehaviour
    {
        protected List<Button> mAllButtonList = new List<Button>();
        protected List<Toggle> mToggleList = new List<Toggle>();
        protected List<InputField> mInputList = new List<InputField>();

        private CanvasGroup mUIMask;
        private Transform mUIContent;
        protected bool mDisableAnim = false;//禁用动画

        private void InitBaseComponent()
        {
            mUIMask = transform.Find("UIMask").GetComponent<CanvasGroup>();
            mUIContent = transform.Find("UIContent").transform;
        }

        #region 生命周期

        public override void OnAwake()
        {
            base.OnAwake();
            InitBaseComponent();
        }

        public override void OnShow()
        {
            base.OnShow();
        }

        public override void OnHide()
        {
            base.OnHide();
        }

        public override void SetVisible(bool isViable)
        {
            gameObject.SetActive(isViable);
            Visible = isViable;
        }
        public void SetMaskVisible(bool isViable)
        {
            //单遮模式
            if (!UISetting.Instance.SINGMASK_SYSTEM)
            {
                return;
            }
            mUIMask.alpha = isViable ? 1 : 0;
        }

        #endregion


        public override void OnDestroy()
        {
            base.OnDestroy();
            RemoveAllButtonListener();
            RemoveAllToggleListener();
            RemoveAllInputFieldListener();
            mAllButtonList.Clear();
            mToggleList.Clear();
            mInputList.Clear();
        }


        #region 事件管理

        public void AddButtonClickListener(Button button, UnityAction action)
        {
            if (button != null)
            {
                if (!mAllButtonList.Contains(button))
                {
                    mAllButtonList.Add(button);
                }
            }
            button.onClick.RemoveAllListeners();
            button.onClick.AddListener(action);
        }
        public void AddToggleClickListener(Toggle toggle, UnityAction<bool, Toggle> action)
        {
            if (toggle != null)
            {
                if (!mToggleList.Contains(toggle))
                {
                    mToggleList.Add(toggle);
                }
                toggle.onValueChanged.RemoveAllListeners();
                toggle.onValueChanged.AddListener((isOn) =>
                {
                    action?.Invoke(isOn, toggle);
                });
            }
        }
        public void AddInputFieldListener(InputField input, UnityAction<string> onChangeAction, UnityAction<string> endAction)
        {
            if (input != null)
            {
                if (!mInputList.Contains(input))
                {
                    mInputList.Add(input);
                }
                input.onValueChanged.RemoveAllListeners();
                input.onEndEdit.RemoveAllListeners();
                input.onValueChanged.AddListener(onChangeAction);
                input.onEndEdit.AddListener(endAction);
            }
        }

        public void RemoveAllButtonListener()
        {
            foreach (var item in mAllButtonList)
            {
                item.onClick.RemoveAllListeners();
            }
        }
        public void RemoveAllToggleListener()
        {
            foreach (var item in mToggleList)
            {
                item.onValueChanged.RemoveAllListeners();
            }
        }
        public void RemoveAllInputFieldListener()
        {
            foreach (var item in mInputList)
            {
                item.onValueChanged.RemoveAllListeners();
                item.onEndEdit.RemoveAllListeners();
            }
        }

        #endregion
        #region 动画管理
        public void ShowAnimation()
        {
            //基础弹窗不需要动画
            if (Canvas.sortingOrder > 90 && mDisableAnim == false)
            {
                //Mask动画
                mUIMask.alpha = 0;
                mUIMask.DOFade(1, 0.2f);
                //缩放动画
                mUIContent.localScale = Vector3.one * 0.8f;
                mUIContent.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack);
            }
        }
        public void HideAnimation()
        {
            if (Canvas.sortingOrder > 90 && mDisableAnim == false)
            {
                mUIContent.DOScale(Vector3.one * 1.1f, 0.2f).SetEase(Ease.OutBack).OnComplete(() =>
                {
                    UIModule.Instance.HideWindow(Name);
                });
            }
            else
            {
                UIModule.Instance.HideWindow(Name);
            }
        }
        #endregion
    }


}